<template>
  <div class="settlement-container">
    <div class="search-section">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="订单号">
          <el-input
            v-model="searchForm.poId"
            placeholder="请输入订单号"
            clearable
          />
        </el-form-item>
        <el-form-item label="供应商">
          <el-input
            v-model="searchForm.supplierName"
            placeholder="请输入供应商名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="settlementList"
      border
      stripe
    >
      <el-table-column prop="poId" label="订单号" min-width="180" />
      <el-table-column prop="supplierName" label="供应商" min-width="120" />
      <el-table-column prop="totalAmount" label="结算金额" width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.totalAmount }}
        </template>
      </el-table-column>
      <el-table-column prop="shipment" label="发货时间" width="180">
        <template slot-scope="scope">
          {{ formatDate(scope.row.shipment) }}
        </template>
      </el-table-column>
      <el-table-column prop="settlementStatus" label="结算状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.settlementStatus === 1 ? 'success' : 'warning'">
            {{ scope.row.settlementStatus === 1 ? '已结算' : '待结算' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.settlementStatus === 0"
            size="mini"
            type="primary"
            @click="handleSettle(scope.row)"
          >
            结算
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-section">
      <el-pagination
        :current-page.sync="pageNum"
        :page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 结算确认对话框 -->
    <el-dialog
      title="确认结算"
      :visible.sync="dialogVisible"
      width="400px"
    >
      <div class="settle-confirm">
        <p>确认为以下订单进行结算？</p>
        <div class="confirm-info">
          <p>订单号：{{ currentOrder?.poId }}</p>
          <p>供应商：{{ currentOrder?.supplierName }}</p>
          <p>结算金额：¥{{ currentOrder?.totalAmount }}</p>
        </div>
      </div>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmSettle" :loading="submitting">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { selectPurchaseSettlementSuppliers, updatePurchaseSettlementStatus } from '@/api/purchaseSettlement'
import { formatDate } from '@/utils/date'

export default {
  name: 'Settlement',
  data() {
    return {
      loading: false,
      submitting: false,
      dialogVisible: false,
      currentOrder: null,
      searchForm: {
        poId: '',
        supplierName: ''
      },
      settlementList: [],
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatDate,
    async getList() {
      this.loading = true
      try {
        const res = await selectPurchaseSettlementSuppliers(this.pageNum)
        if (res.code === 200) {
          this.settlementList = res.data.list || []
          this.total = res.data.total || 0
        }
      } catch (error) {
        console.error('获取结算列表失败:', error)
        this.$message.error('获取结算列表失败')
      } finally {
        this.loading = false
      }
    },
    handleSearch() {
      this.pageNum = 1
      this.getList()
    },
    resetSearch() {
      this.searchForm = {
        poId: '',
        supplierName: ''
      }
      this.handleSearch()
    },
    handleSettle(row) {
      this.currentOrder = row
      this.dialogVisible = true
    },
    async confirmSettle() {
      if (!this.currentOrder) return
      
      this.submitting = true
      try {
        const res = await updatePurchaseSettlementStatus(
          this.currentOrder.poId,
          this.currentOrder.supplierId
        )
        
        if (res.code === 200) {
          this.$message.success('结算成功')
          this.dialogVisible = false
          this.getList()
        }
      } catch (error) {
        console.error('结算失败:', error)
        this.$message.error('结算失败')
      } finally {
        this.submitting = false
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.settlement-container {
  padding: 20px;

  .search-section {
    background: #fff;
    padding: 18px 20px;
    border-radius: 4px;
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  }

  .pagination-section {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
  }

  .settle-confirm {
    padding: 20px;
    text-align: center;

    .confirm-info {
      margin-top: 20px;
      text-align: left;
      padding: 15px;
      background: #f5f7fa;
      border-radius: 4px;

      p {
        margin: 8px 0;
        color: #606266;
      }
    }
  }
}
</style>